﻿
namespace Fayshine.VA.RIAService.Web
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Data;
    using System.Linq;
    using System.ServiceModel.DomainServices.EntityFramework;
    using System.ServiceModel.DomainServices.Hosting;
    using System.ServiceModel.DomainServices.Server;


    // Implements application logic using the FayshineEntities context.
    // TODO: Add your application logic to these methods or in additional methods.
    // TODO: Wire up authentication (Windows/ASP.NET Forms) and uncomment the following to disable anonymous access
    // Also consider adding roles to restrict access as appropriate.
    // [RequiresAuthentication]
    [EnableClientAccess()]
    public class FayshineDomainService : LinqToEntitiesDomainService<FayshineEntities>
    {

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'roles' query.
        [Query(IsDefault = true)]
        public IQueryable<role> GetRoles()
        {
            return this.ObjectContext.roles.Include("users");
        }

        public void InsertRole(role role)
        {
            if ((role.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(role, EntityState.Added);
            }
            else
            {
                this.ObjectContext.roles.AddObject(role);
            }
        }

        public void UpdateRole(role currentrole)
        {
            this.ObjectContext.roles.AttachAsModified(currentrole, this.ChangeSet.GetOriginal(currentrole));
        }

        public void DeleteRole(role role)
        {
            if ((role.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(role, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.roles.Attach(role);
                this.ObjectContext.roles.DeleteObject(role);
            }
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'users' query.
        [Query(IsDefault = true)]
        public IQueryable<user> GetUsers()
        {
            return this.ObjectContext.users.Include("role");
        }

        public void InsertUser(user user)
        {
            if ((user.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(user, EntityState.Added);
            }
            else
            {
                this.ObjectContext.users.AddObject(user);
            }
        }

        public void UpdateUser(user currentuser)
        {
            this.ObjectContext.users.AttachAsModified(currentuser, this.ChangeSet.GetOriginal(currentuser));
        }

        public void DeleteUser(user user)
        {
            if ((user.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(user, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.users.Attach(user);
                this.ObjectContext.users.DeleteObject(user);
            }
        }
    }
}


